import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import {ChatMessageDto} from './dto/chatMessage.dto'
import { ChatMessage} from './entities/chatMessage.entity'
@Injectable()
export class ChatMessageService {
  constructor(
    @InjectModel('ChatMessage') private readonly chatMessageModel:Model<ChatMessage>
) {}
  async create(chatMessageDto: ChatMessageDto) {
    return await this.chatMessageModel.insertMany(chatMessageDto);
  }
  async find(sessionId: string, id: string):Promise<ChatMessage[]> {
    if(id)return await this.chatMessageModel.find({_id:id});
    if(sessionId)return await this.chatMessageModel.find({sessionId:sessionId});
  }

  async update(id: string, chatMessageDto: ChatMessageDto) {
    return await this.chatMessageModel.updateOne({_id:id}, chatMessageDto);
  }

  async remove(id: string):Promise<any> {
    return await this.chatMessageModel.deleteOne({_id:id});
  }

  async list(page: number, pageSize: number):Promise<any> {
    const skip = (page - 1) * pageSize;
    const data = await this.chatMessageModel.find().sort({ time: -1 })
    // const list = await this.chatMessageModel.find().sort({ time: -1 }).skip(skip).limit(pageSize)
    return {total: data.length, list: data}
  }
}
